﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects;
using System.Data.SqlClient;
using DataAccess;
using InfrastructureSqlServer.Helpers;
using System.Configuration;
using System.Data;

namespace DataAccess
{
    public class EventoDAO
    {
        private List<Evento> CarregarListaEvento(SqlDataReader dr)
        {
            List<Evento> liEvento = new List<Evento>();

            while (dr.Read())
            {
                Evento evento = new Evento();
                evento.EventoId = int.Parse(dr["EventoId"].ToString());
                evento.Descricao = dr["Descricao"].ToString();
                evento.Ativo = int.Parse(dr["Ativo"].ToString());
                evento.Operacao = int.Parse(dr["Operacao"].ToString());
                liEvento.Add(evento);
            }

            return liEvento;
        }

        public void Inserir(Evento _evento)
        {
            SqlParameter[] paramsToSP = new SqlParameter[3];

            paramsToSP[0] = new SqlParameter("@descricao", _evento.Descricao);
            paramsToSP[1] = new SqlParameter("@ativo", _evento.Ativo);
            paramsToSP[2] = new SqlParameter("@operacao", _evento.Operacao);

            try
            {
                SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.StoredProcedure, "stp_insert_evento", paramsToSP);

            }
            catch (Exception e)
            {
                throw new Exception("Erro ao inserir evento, Erro: " + e.Message);
            }

        }

        public void Alterar(Evento _evento)
        {
            SqlParameter[] paramsToSP = new SqlParameter[4];

            paramsToSP[0] = new SqlParameter("@eventoid", _evento.EventoId);
            paramsToSP[1] = new SqlParameter("@descricao", _evento.Descricao);
            paramsToSP[2] = new SqlParameter("@ativo", _evento.Ativo);
            paramsToSP[3] = new SqlParameter("@operacao", _evento.Operacao);

            try
            {
                SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.StoredProcedure, "stp_update_evento", paramsToSP);
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao alterar evento, Erro: " + e.Message);
            }

        }

        public bool Deletar(int _eventoId)
        {
            SqlParameter[] paramsToSP = new SqlParameter[1];

            paramsToSP[0] = new SqlParameter("@eventoid", _eventoId);

            try
            {
                SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.StoredProcedure, "stp_delete_evento", paramsToSP);
                return true;
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao deletar evento, Erro: " + e.Message);
            }

        }

        public List<Evento> Consultar(int _eventoId)
        {
            string sqlQuery = string.Format("SELECT * FROM EVENTO WHERE EVENTOID = {0}", _eventoId);

            try
            {
                return CarregarListaEvento(SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.Text, sqlQuery));
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao consultar evento, Erro: " + e.Message);
            }

        }

        public List<Evento> Consultar(string _evento)
        {
            string sqlQuery = string.Format("SELECT * FROM EVENTO WHERE descricao like '%{0}%'", _evento);

            try
            {
                return CarregarListaEvento(SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.Text, sqlQuery));
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao consultar evento, Erro: " + e.Message);
            }

        }

        public List<Evento> Consultar()
        {
            string sqlQuery = string.Format("SELECT * FROM EVENTO WHERE ativo = 1");

            try
            {
                return CarregarListaEvento(SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.Text, sqlQuery));
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao consultar evento, Erro: " + e.Message);
            }

        }
    }//class
}//namespace
